package com.pipayshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.domain.ShopFollowFocus;
import com.pipayshop.domain.vo.FansVO;
import com.pipayshop.mapper.OnlineStoreShopFollowFocusMapper;
import com.pipayshop.service.OnlineStoreShopFollowFocusSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author xiaoqiu
 * @date 2024/9/23 10:36
 * @description
 */
@Service
public class OnlineStoreShopFollowFocusSerivceImpl extends ServiceImpl<OnlineStoreShopFollowFocusMapper, ShopFollowFocus> implements OnlineStoreShopFollowFocusSerivce {

    @Autowired
    private OnlineStoreShopFollowFocusMapper shopFollowFocusMapper;

    /**
     * 用户关注网店接口
     *
     * @param followId
     * @param shopId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean userFollowShop(String followId, String shopId) {
        Date date = new Date();
        // 根据用户id和网店id查询关注记录
        ShopFollowFocus shopFollowFocus = shopFollowFocusMapper.selectOne(
                new QueryWrapper<ShopFollowFocus>()
                        .eq("follow_id", followId)
                        .eq("shop_id", shopId)
        );
        // 处理不同的情况
        if (shopFollowFocus != null) {
            int updateStatus = shopFollowFocus.getStatus() == 0 ? 1 : 0;
            int update = shopFollowFocusMapper.update(null,
                    new UpdateWrapper<ShopFollowFocus>()
                            .eq("follow_id", followId)
                            .eq("shop_id", shopId)
                            .set("update_time", date)
                            .set("status", updateStatus));

            return update == 0 ? true : false;
        } else {
            // 否则是新增关注记录
            ShopFollowFocus itemFollowFocus = new ShopFollowFocus();
            itemFollowFocus.setFollowId(followId);
            itemFollowFocus.setShopId(shopId);
            itemFollowFocus.setCreateTime(date);
            itemFollowFocus.setUpdateTime(date);
            itemFollowFocus.setStatus(0);

            int insert = shopFollowFocusMapper.insert(itemFollowFocus);
            return insert > 0;
        }

    }

    @Override
    public List<FansVO> shopFans(String shopId, Integer pageNum, Integer pageSize) {
        List<FansVO> fansVOList = shopFollowFocusMapper.shopFans(shopId, pageNum*pageSize, pageSize);
        return fansVOList;
    }

    @Override
    public Long shopFansSum(String shopId) {
        LambdaQueryWrapper<ShopFollowFocus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopFollowFocus::getShopId, shopId)
                    .eq(ShopFollowFocus::getStatus, 0);
        return shopFollowFocusMapper.selectCount(queryWrapper);

    }

    @Override
    public boolean isFollowShop(String shopId, String userId) {
        int i = shopFollowFocusMapper.selectCount(new QueryWrapper<ShopFollowFocus>().eq("shop_id", shopId)
                .eq("follow_id", userId).eq("status", 0)).intValue();
        return i == 1;
    }


}
